package com.hsyt.pms.security.rsa.util;

import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.Signature;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.text.MessageFormat;
import org.apache.commons.codec.binary.Base64;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class RSASignature {
    private static final Logger logger = LoggerFactory.getLogger(RSASignature.class);
    public static final String SIGN_ALGORITHMS = "SHA1WithRSA";

    public RSASignature() {
    }

    public static String sign(String content, String privateKey, String encode) {
        logger.debug("【RSA签名】数据签名 content：{0} privateKey：{1} encode：{2}", new Object[]{content, privateKey, encode});

        try {
            PKCS8EncodedKeySpec priPKCS8 = new PKCS8EncodedKeySpec(Base64.decodeBase64(privateKey));
            KeyFactory keyf = KeyFactory.getInstance("RSA");
            PrivateKey priKey = keyf.generatePrivate(priPKCS8);
            Signature signature = Signature.getInstance("SHA1WithRSA");
            signature.initSign(priKey);
            signature.update(content.getBytes(encode));
            byte[] signed = signature.sign();
            return Base64.encodeBase64String(signed);
        } catch (Exception var8) {
            logger.error(MessageFormat.format("【RSA签名】数据签名失败 content：{0} privateKey：{1} encode：{2}", content, privateKey, encode), var8);
            return null;
        }
    }

    public static String sign(String content, String privateKey) {
        try {
            PKCS8EncodedKeySpec priPKCS8 = new PKCS8EncodedKeySpec(Base64.decodeBase64(privateKey));
            KeyFactory keyf = KeyFactory.getInstance("RSA");
            PrivateKey priKey = keyf.generatePrivate(priPKCS8);
            Signature signature = Signature.getInstance("SHA1WithRSA");
            signature.initSign(priKey);
            signature.update(content.getBytes());
            byte[] signed = signature.sign();
            return Base64.encodeBase64String(signed);
        } catch (Exception var7) {
            logger.error(MessageFormat.format("【RSA签名(没有编码入参)】数据签名失败 content：{0} privateKey：{1}", content, privateKey), var7);
            return null;
        }
    }

    public static boolean doCheck(String content, String sign, String publicKey, String encode) {
        try {
            KeyFactory keyFactory = KeyFactory.getInstance("RSA");
            byte[] encodedKey = Base64.decodeBase64(publicKey);
            PublicKey pubKey = keyFactory.generatePublic(new X509EncodedKeySpec(encodedKey));
            Signature signature = Signature.getInstance("SHA1WithRSA");
            signature.initVerify(pubKey);
            signature.update(content.getBytes(encode));
            boolean bverify = signature.verify(Base64.decodeBase64(sign));
            return bverify;
        } catch (Exception var9) {
            logger.error(MessageFormat.format("【RSA验签名检查】数据签名失败 content：{0} sign：{1} publicKey：{2} encode:{3}", content, sign, publicKey, encode), var9);
            return false;
        }
    }

    public static boolean doCheck(String content, String sign, String publicKey) {
        try {
            KeyFactory keyFactory = KeyFactory.getInstance("RSA");
            byte[] encodedKey = Base64.decodeBase64(publicKey);
            PublicKey pubKey = keyFactory.generatePublic(new X509EncodedKeySpec(encodedKey));
            Signature signature = Signature.getInstance("SHA1WithRSA");
            signature.initVerify(pubKey);
            signature.update(content.getBytes());
            boolean bverify = signature.verify(Base64.decodeBase64(sign));
            return bverify;
        } catch (Exception var8) {
            logger.error(MessageFormat.format("【RSA验签名检查(无编码参数)】数据签名失败 content：{0} sign：{1} publicKey：{2}", content, sign, publicKey), var8);
            return false;
        }
    }
}
